String.prototype.format = function () {
    if (arguments.length == 0) return this;
    var param = arguments[0];
    var s = this;
    if (typeof(param) == 'object') {
        for (var key in param)
            s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
        return s;
    } else {
        for (var i = 0; i < arguments.length; i++)
            s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
        return s;
    }
}

Date.prototype.formatDate = function (formatStr) {
    var date = this;
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) month = "0" + month;
    if (day < 10) day = "0" + day;
    return year + "-" + month + "-" + day;
};

Date.prototype.formatTime = function (formatStr) {
    var date = this;
    var hours = date.getHours();
    var mins = date.getMinutes();
    var secs = date.getSeconds();
    var msecs = date.getMilliseconds();
    if (hours < 10) hours = "0" + hours;
    if (mins < 10) mins = "0" + mins;
    if (secs < 10) secs = "0" + secs;
    if (msecs < 10) secs = "0" + msecs;
    return date.formatDate() + " " + hours + ":" + mins + ":" + secs;
};

//过滤重复元素
function arrayDistinct(array) {
    var res = [];
    if (Array.isArray(array)) {
        for (var i in array) {
            if ($.inArray(array[i], res) < 0) {
                res.push(array[i]);
            }
        }
    }
    return res;
};

function getRandom(n, m) {
    var random = Math.floor(Math.random() * (m - n + 1) + n);
    return random;
};

//设置css中url加入MD5
function resetCss(oldcss) {
    var newcss = oldcss;
    var re = /resources\/[^\s]*\)/g;
    var array = arrayDistinct(newcss.match(re)) || [];
    //console.log(array);
    for (var i in array) {
        if (array[i]) {
            var end = (array[i])[array[i].length - 2];
            var key = "";
            if (end === "\'" || end === "\"") {
                key = array[i].substring(0, array[i].length - 2);
            } else {
                key = array[i].substring(0, array[i].length - 1);
            }
            //console.log("array key:" + key);
            newcss = newcss.replace(new RegExp(key, "g"), key.getPathMd5());
        }

    }
    return newcss;
};

//加入基础css,存在于整个生命周期
function addOnceStyle(css) {
    $('<style type = "text/css"></style>').text(resetCss(css)).insertBefore($("#headStyle"));
    //	var style = document.createElement('style');
    //	style.type = "text/css";
    //	style.innerHTML = resetCss(css);
    //	document.getElementsByTagName('head').item(0).appendChild(style);
};

define([
    "text!css/common/base.css",
    "text!css/common/global.menu.css",
    "text!css/common/global.popup.css",
], function () {
    var array = arguments;
    return function () {
        for (var i in array) {
            addOnceStyle(array[i]);
        }
    }
});